/** \file
* Declarations for the portable Timer class.
* This file is part of Chorus, available at
* http://code.google.com/p/chorus-twitter/ . This file and all associated files
* and derivative works are bound by the terms of the LGPL 3.0 license,
* available at http://www.gnu.org/licenses/lgpl-3.0-standalone.html .
* \par Subversion URL:
* \$URL: http://chorus-twitter.googlecode.com/svn/branches/cpp/src/system/Timer.hpp $
* \version \$Rev: 152 $
* \date    \$Date: 2010-04-06 07:35:13 +0000 (Tue, 06 Apr 2010) $
*/

#pragma once

#ifdef WIN32
#	include <windows.h>
#endif

#ifdef POSIX
#	include <time.h>
#endif

#ifdef __APPLE__
#	include <mach/mach_host.h>
#endif

/// A timer object that counts up from 0. Uses reasonably high-resolution timer
/// code in both posix and win32.
class Timer
{
public:
	/// The constructor.
	Timer();

public:
	/// Get the time in seconds since construction.
	/// \return The time, in seconds.
	long double getTimeDouble() const;

#ifdef POSIX
	/// Get the absolute system time as a timespec (seconds and nanoseconds).
	/// \return The timespec time.
	timespec getTimeSpec() const;

	/// Convert a millisecond time value to a timespec time value.
	/// \param ms The time in milliseconds.
	/// \return The timespec time.
	static timespec msToTs(unsigned ms);

	/// Add a timespec and a millisecond time.
	/// \param ts The timespec time.
	/// \param ms The millisecond time.
	/// \return The sum, as a timespec.
	static timespec addSpecMs(timespec ts, unsigned ms);
#endif

private:

#ifdef WIN32
	/// The initial time offset.
	LARGE_INTEGER time0;
	/// The high-performance counter frequency.
	LARGE_INTEGER freq;
#endif

#ifdef POSIX
	/// The initial time offset.
	timespec time0;
#endif
	
#ifdef __APPLE__
	/// The apple kernel host clock object.
	clock_serv_t hostclock;
#endif
};

/// The global timer, relative to the start of the program. Must be
/// instantiated by main().
extern Timer *timerglob;
